package com.example.listeners;

import java.util.ArrayList;
import java.util.Arrays;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.example.event.Event;
import com.example.event.OnClickEvent;
import com.example.event.OnTouchEvent;
import com.example.handler.ClientHandler;

public class MyOnTouchListener extends MyListener implements OnTouchListener {
	
	private ArrayList<int[]> pointList;
	private int[] prevPoint;

	public MyOnTouchListener(ClientHandler client) {
		super(client);
		pointList = new ArrayList<int[]>();
	}
	
	public ArrayList<int[]> getPointList() {
		return pointList;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		
		int[] point = {(int) event.getX(), (int) event.getY()};

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			pointList.clear();
			pointList.add(point);
			prevPoint = point;
			Event downEvent = new OnTouchEvent(point);
			((OnTouchEvent)downEvent).setAction("ACTION_DOWN");
			client.eventOccured(downEvent);
			break;
		case MotionEvent.ACTION_MOVE:
			if(!Arrays.equals(point, prevPoint)) {
				pointList.add(point);
				prevPoint = point;
			}
			Event moveEvent = new OnTouchEvent(point);
			((OnTouchEvent)moveEvent).setAction("ACTION_MOVE");
			client.eventOccured(moveEvent);
			break;
		case MotionEvent.ACTION_UP:
			if(!Arrays.equals(point, prevPoint)) {
				pointList.add(point);
				prevPoint = null;
			}
			// OnTouchEvent
			Event touchEvent = new OnTouchEvent(point);
			((OnTouchEvent)touchEvent).setPointList(pointList);
			((OnTouchEvent)touchEvent).setAction("ACTION_UP");
			client.eventOccured(touchEvent);
			break;
		}

		// TODO Auto-generated method stub
		return false;
	}
}
